<?php

namespace App\Models\Api;

class BrowseJob extends ApiModel
{
    //表名
    protected $table = 'browse_job';

    protected $guarded = [];

    /**
     * The attributes that should be hidden for arrays.
     * 隐藏字段
     * @var array
     */
    protected $hidden = ['created_at', 'updated_at', 'resume', 'company_basic'];

    public function company_basic()
    {
        return $this->hasOne(CompanyBasic::class, 'id', 'com_id')->select('id', 'company_name', 'logo');
    }

    public function resume()
    {
        return $this->hasOne(Resume::class, 'id', 'resume_id')
            ->where('resume_status', 1)
            ->with(['homeaddress_area_name'])
            ->select('id', 'name', 'sex', 'birthday', 'education',
                'updatetime', 'intentionjobs', 'avatar', 'homeaddress',
                'chkphoto_open');
    }

    public function company_job()
    {
        return $this->hasOne(CompanyJob::class, 'id', 'job_id')
            ->whereBetween('job_status', [0, 2])
            ->select('id', 'com_id', 'job_title', 'salary_above', 'salary_below',
                'education', 'work_year', 'location1', 'location2', 'location3',
                'location4', 'location5', 'updatetime', 'urgent');
    }

    public function getTimeAttribute()
    {
        return $this->updated_at->format('Y-m-d');
    }

    //头像
    public function getAvatarAttribute()
    {
        if (!$this->resume || $this->resume->chkphoto_open == 1) {//隐藏头像
            return '';
        }
        return $this->resume->avatar;
    }

    public function getNameAttribute()
    {
        return $this->resume ? $this->resume->name : '';
    }

    public function getSexAttribute()
    {
        return $this->resume ? $this->resume->sex : '';
    }

    public function getAgeAttribute()
    {
        return $this->resume ? $this->resume->age : '';
    }

    public function getIntentionjobsAttribute()
    {
        return $this->resume ? $this->resume->intentionjobs : '';
    }

    public function getEducationNameAttribute()
    {
        return $this->resume ? $this->resume->education_name : '';
    }

    public function getHomeaddressNameAttribute()
    {
        return $this->resume ? $this->resume->homeaddress_name : '';
    }
}
